<template>
  <a-card title="人才智库" :bordered="false" class="br-10">
    <div class="table-page-search">
      <a-form>
        <a-form-item label="名称">
          <a-input placeholder="请输入" v-model="queryParam.keyWord" />
        </a-form-item>
        <a-form-item label="职务">
          <a-select v-model="queryParam.type" placeholder="请选择职务">
            <a-select-option value="">全部</a-select-option>
            <a-select-option :value="item.dictValue" v-for="(item, index) in titleOption" :key="index">{{
              item.dictLabel
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="状态">
          <a-select v-model="queryParam.status" default-value="">
            <a-select-option value="">全部</a-select-option>
            <a-select-option value="1">启用</a-select-option>
            <a-select-option value="0">禁用</a-select-option>
          </a-select>
        </a-form-item>
        <span class="search-submitButtons">
          <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
          <a-button @click="() => (queryParam = { status: '', type: '' })">重置</a-button>
        </span>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button type="primary" @click="handleAdd">新增</a-button>
      <a-button @click="templateDown">下载模板</a-button>
      <a-upload
        name="file"
        :customRequest="upload"
        :headers="headers"
        :before-upload="beforeUpload"
        :showUploadList="false"
      >
        <a-button>导入</a-button>
      </a-upload>
      <a-button @click="excelDown">导出</a-button>
    </div>
    <s-table size="default" ref="table" rowKey="userId" :rowSelection="null" :columns="columns" :data="loadData">
      <span slot="serial" slot-scope="text, record, index">
        {{ (pageNum - 1) * pageSize + index + 1 }}
      </span>
      <span slot="image" slot-scope="text, record">
        <a-avatar v-if="record.image !== ''" :src="record.image" />
        <a-avatar v-else icon="user" />
      </span>
      <span slot="enabled" slot-scope="text, record">
        <a-switch :checked="record.enabled === 1" @change="onChangeStatus(record)" />
      </span>
      <span slot="action" slot-scope="text, record">
        <a-button type="primary" ghost size="small" class="_mr-5" @click="handleEdit(record)">
          编辑
        </a-button>
        <a-button type="primary" ghost size="small" class="_mr-5" @click="delByIds(record, [record.id])">
          删除
        </a-button>
      </span>
    </s-table>
    <thinkTankForm ref="thinkTankForm" @add="$refs.table.refresh(true)" @upd="$refs.table.refresh()" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import {
  thinkTankPageList,
  thinkTankDelete,
  thinkTankEnable,
  thinkTankExportTemplate,
  thinkTankExportExcel,
  thinkTankImportExcel
} from '@/api/web/biz'
import { dataType } from '@/api/system/system'
import thinkTankForm from './modules/thinkTankForm'
import { mimeMap, readAsText, resolveBlob } from '@/utils/download'
import Vue from 'vue'
import { ACCESS_TOKEN, TENANTID } from '@/store/mutation-types'
export default {
  components: {
    STable,
    thinkTankForm
  },
  data() {
    return {
      headers: {
        token: Vue.ls.get(ACCESS_TOKEN),
        tenantId: Vue.ls.get(TENANTID)
      },
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      titleOption: [],

      // 查询参数
      queryParam: { status: '', type: '' },
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          width: '80px'
        },
        {
          title: '头像',
          dataIndex: 'image',
          width: '100px',
          scopedSlots: { customRender: 'image' }
        },
        {
          title: '姓名',
          dataIndex: 'name',
          width: '200px',
          ellipsis: true
        },
        {
          title: '职务',
          dataIndex: 'typeStr',
          width: '200px'
        },
        {
          title: '个人简介',
          dataIndex: 'remarks',
          width: '200px',
          ellipsis: true
        },
        {
          title: '排序',
          dataIndex: 'sort',
          width: '100px'
        },
        {
          title: '状态',
          dataIndex: 'enabled',
          scopedSlots: { customRender: 'enabled' },
          width: '200px'
        },
        {
          title: '操作',
          width: '150px',
          fixed: 'right',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      pageNum: 1,
      pageSize: 10,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        var params = {
          current: parameter.pageNum,
          size: parameter.pageSize
        }
        return thinkTankPageList(Object.assign(params, this.queryParam)).then(res => {
          this.pageNum = parameter.pageNum
          this.pageSize = parameter.pageSize
          this.selectedRowKeys = []
          this.selectedRows = []
          this.ids = []
          return res
        })
      }
    }
  },
  created() {
    this.handletitleData()
  },
  methods: {
    // 获取职务
    handletitleData() {
      var params = {
        dictType: 'think_tank_type'
      }
      return dataType(params).then(res => {
        this.titleOption = res.data
      })
    },

    onChangeStatus(record) {
      var msg = ''
      if (record.enabled === 0) {
        msg = '启用成功'
      } else {
        msg = '禁用成功'
      }
      record.status = record.enabled === 0 ? 1 : 0
      thinkTankEnable(record).then(res => {
        this.$message.success(msg, 1)
        this.$refs.table.refresh()
      })
    },

    // 下载模板
    templateDown() {
      thinkTankExportTemplate()
    },

    // 导入前校验
    beforeUpload(file) {
      const bool = file.type === mimeMap.xls || file.type === mimeMap.xlsx;
      if (!bool) {
        this.$message.error('请上传excel文件!')
      }
      return bool
    },

    // 导入模板
    upload(params) {
      const file = params.file
      // 根据后台需求数据格式
      var form = new FormData()
      // 文件对象
      form.append('file', file)
      return thinkTankImportExcel(form).then(res => {
        console.log('res.====', res.data.type)
        if (res.data.type === 'application/json') {
          readAsText(res.data).then(result => {
            this.$message.success('导入成功')
            this.$refs.table.refresh(true)
          })
        } else {
          this.$refs.table.refresh(true)
          resolveBlob(res, mimeMap.xlsx)
        }
      })
    },

    // 导出
    excelDown() {
      thinkTankExportExcel(this.queryParam)
    },

    handleAdd() {
      var params = {
        title: '新增人员',
        type: 'add'
      }
      this.$refs.thinkTankForm.add(params)
    },
    handleEdit(record) {
      var params = {
        title: '编辑人员',
        type: 'edit',
        data: record
      }
      this.$refs.thinkTankForm.edit(params)
    },

    handleOk() {
      this.$refs.table.refresh(true)
    },
    handleReload() {
      this.$refs.table.refresh()
    },
    delByIds(obj, ids) {
      var _this = this
      this.$confirm({
        title: '删除?',
        content: `是否确定删除"${obj.name}"吗?`,
        onOk() {
          thinkTankDelete({ ids: ids.join(',') }).then(res => {
            _this.$message.success(`删除成功`)
            _this.handleReload()
          })
        },
        onCancel() {}
      })
    }
  }
}
</script>
<style scoped lang="less"></style>
